﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LightPersist;
using System.IO;
using LightPersist.Metainfo;
using System.Windows.Markup;
using System.Threading;
using Dom;
using LightPersist.Storage;

namespace LightPersistTest.ComplexUI
{

	public partial class Window1
	{
		LightPersistContext Ctx;
		Root Root;
		LightPersist.Transactions.TransactionGuard Transaction;
		Random Rnd;

		public Window1()
		{
			var storage = new InmemoryStorage();
			storage.Connect("what should I write here?");
			Ctx = new LightPersistContext(storage, DomMetainfo.Metainfo);


			using (var transaction = Ctx.BeginTransaction())
			{
				Root = Ctx.RootClass<Dom.Root>();

				Root.Clipboard = Ctx.Create<Dom.Clipboard>();


				Rnd = new Random();

				for (int i = 0; i < 5; i++)
				{
					var ctrl = CreateControl(Root.Children, null);

					for (int j = 0; j < Rnd.Next(0, 3); j++)
					{
						CreateControl(ctrl.Children, ctrl);
					}
				}

				InitializeComponent();
				DataContext = Root;
				transaction.CommitTransaction();
			}

			Transaction = Ctx.BeginTransaction();

			this.TargetUpdated += new EventHandler<DataTransferEventArgs>(Window1_TargetUpdated);
			this.SourceUpdated += new EventHandler<DataTransferEventArgs>(Window1_SourceUpdated);
			//Binding.TargetUpdatedEvent+=new RoutedEventHandler(some);

		}

		void Window1_SourceUpdated(object sender, DataTransferEventArgs e)
		{

			Debug.WriteLine(sender.ToString() + " args: " + e.ToString());

			Transaction.CommitTransaction();

			Transaction = Ctx.BeginTransaction();
		}

		void Window1_TargetUpdated(object sender, DataTransferEventArgs e)
		{
			Debug.WriteLine(sender.ToString() + " args: " + e.ToString());
		}


		int index = 0;

		Dom.Control CreateControl(IList<Dom.Control> whereToAdd, Dom.Control parent)
		{
			var ctrl = Ctx.Create<Dom.Control>();
			whereToAdd.Add(ctrl);

			ctrl.Name = "Control " + index;
			ctrl.Width = index * 10;
			ctrl.Height = index * 5;
			ctrl.IsVisible = (index & 1) == 1;
			ctrl.Parent = parent;

			if (ctrl.IsVisible)
			{
				// add triggers
				for (int t = 0; t < Rnd.Next(1, 6); t++)
				{
					var trigger = Ctx.Create<Dom.Trigger>();
					Root.Triggers.Add(trigger);
					// link triggers and cobtrols together
					trigger.Target = ctrl;
					ctrl.Triggers.Add(trigger);

					trigger.Condition = "Trigger " + t;
					trigger.ConditionValue = t * 10;
					trigger.IsActive = true;

				}

			}

			index++;
			return ctrl;
		}

		private void delete_Click(object sender, RoutedEventArgs e)
		{
			if (listTriggers.SelectedIndex != -1)
			{
				var tr = (Dom.Trigger)listTriggers.SelectedValue;
				Root.Triggers.Remove(tr);
			}

			Transaction.CommitTransaction();

			Transaction = Ctx.BeginTransaction();
		}

		private void deleteCtrol_Click(object sender, RoutedEventArgs e)
		{
			Transaction.CommitTransaction();

			Transaction = Ctx.BeginTransaction();

			if (treeView1.SelectedValue != null)
			{
				var ctrl = (Dom.Control)treeView1.SelectedValue;
				if (ctrl.Parent != null)
				{
					var parent = ctrl.Parent;
					ctrl.Parent = null;
					parent.Children.Remove(ctrl);
				}
			}

			Transaction.CommitTransaction();

			Transaction = Ctx.BeginTransaction();

		}

		private void addCtrl_Click(object sender, RoutedEventArgs e)
		{


			Transaction.CommitTransaction();
			Transaction = Ctx.BeginTransaction();
			if (treeView1.SelectedValue != null)
			{
				var ctrl = (Dom.Control)treeView1.SelectedValue;
				CreateControl(ctrl.Children, ctrl);
			}
			Transaction.CommitTransaction();

			Transaction = Ctx.BeginTransaction();

		}

		private void moveCtrl_Click(object sender, RoutedEventArgs e)
		{

			Transaction.CommitTransaction();
			Transaction = Ctx.BeginTransaction();

			if (Root.Triggers.Count >= 3)
			{
				for (int i = 0; i < 3; i++)
				{
					var tr = Root.Triggers[0];
					Root.Clipboard.Children.Add(tr);
					Root.Triggers.RemoveAt(0);
				}
			}
			if (Root.Children.Count >= 2)
			{
				for (int i = 0; i < 2; i++)
				{
					var ctrl = Root.Children[0];
					Root.Clipboard.Children.Add(ctrl);
					Root.Children.RemoveAt(0);
				}
			}


			Transaction.CommitTransaction();
			Transaction = Ctx.BeginTransaction();
		}

		private void undo_Click(object sender, RoutedEventArgs e)
		{
			Transaction.CommitTransaction();

			if (Ctx.CanUndo)
				Ctx.Undo();

			Transaction = Ctx.BeginTransaction();
		}

		private void Redo_Click(object sender, RoutedEventArgs e)
		{
			Transaction.CommitTransaction();


			if (Ctx.CanRedo)
				Ctx.Redo();

			Transaction = Ctx.BeginTransaction();

		}

	}
}
